package sedev.struts.portlet;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import sedev.shopping.business.DonViGiaService;
import sedev.shopping.business.LoaiSanPhamService;
import sedev.shopping.business.SanPhamService;
import sedev.shopping.entities.ListDonViGia;
import sedev.shopping.entities.ListLoaiSanPham;
import sedev.shopping.entities.ListSanPham;
import sedev.shopping.entities.LoaiSanPham;
import sedev.shopping.entities.SanPham;
import sedev.shopping.util.ConfigUtil;
import sedev.shopping.util.Paging;
import sedev.shopping.util.SedevUtil;
import sedev.shopping.util.SedevUtil.ModeDisplay;

import com.liferay.portal.util.PortalUtil;
import com.opensymphony.xwork2.ActionSupport;

public class ProductAction extends ActionSupport implements ServletRequestAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private Long ownerId;
	private Boolean isAdmin;
	
	private int preExecute(){
		if(SedevUtil.isOwner(this.request) == false)
			return -1;
		
		return 0;
	}
	
	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO Auto-generated method stub
		this.request = request;
		this.ownerId = PortalUtil.getUserId(this.request);
		this.isAdmin = SedevUtil.isAdministrator(this.ownerId);
	}
	
	@Override
	public  String execute() throws Exception {   
		if(this.preExecute() < 0){
			return "denied";
		}
		
		String actionResult = "";
		Paging paging = new Paging();
		if(this.request.getParameter("shopping_page_manager") != null){
			paging.setCurrent(Integer.valueOf(this.request.getParameter("shopping_page_manager")));
		}
				
		paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.pagesize","5")));
		paging.setNumDisplay(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.numdisplay","10")));
		ListSanPham listdata = SanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.All);
		this.request.setAttribute("listsanpham", listdata);
		this.request.setAttribute("paging", paging);
		actionResult = ActionSupport.SUCCESS;
		return actionResult;       
    }
	
	public String post() throws Exception{
		if(this.preExecute() < 0){
			return "denied";
		}
		Paging paging = new Paging();
		ListLoaiSanPham listloaisanpham = LoaiSanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.Display);
		ListDonViGia listdonvigia = DonViGiaService.getListDataByAll();		
		this.request.setAttribute("listloaisanpham", listloaisanpham);
		this.request.setAttribute("listdonvigia", listdonvigia);
		
		this.request.setAttribute("currentdate", SedevUtil.getTime());
		return ActionSupport.SUCCESS;
	}
	
	public String edit() throws Exception{
		if(this.preExecute() < 0){
			return "denied";
		}
		SanPham data = new SanPham();
		try {
			Long id = Long.valueOf((String) this.request.getParameter("productid"));
			data = SanPhamService.getData(id);
			if (data != null) {
				this.request.setAttribute("sanpham", data);
				Paging paging = new Paging();
				ListLoaiSanPham listloaisanpham = LoaiSanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.Display);
				ListDonViGia listdonvigia = DonViGiaService.getListDataByAll();		
				this.request.setAttribute("listloaisanpham", listloaisanpham);
				this.request.setAttribute("listdonvigia", listdonvigia);
				return ActionSupport.SUCCESS;
			} else{
				this.request.setAttribute("errorCode", -1);
			}
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return index page
		Paging paging = new Paging();
		paging.setCurrent(1);
		paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.pagesize","5")));
		paging.setNumDisplay(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.numdisplay","10")));
		ListSanPham listdata = SanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.All);
		this.request.setAttribute("listsanpham", listdata);
		this.request.setAttribute("paging", paging);
		return ActionSupport.ERROR;
	}
	
	public String add() throws Exception{
		if(this.preExecute() < 0){
			return "denied";
		}
		SanPham data = new SanPham();
		try {
			if (this.initData(data)) {
				if (SanPhamService.addData(data)) {
					this.request.setAttribute("errorCode", 1);
					this.request.setAttribute("sanpham", data);
					Paging paging = new Paging();
					ListLoaiSanPham listloaisanpham = LoaiSanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.Display);
					ListDonViGia listdonvigia = DonViGiaService.getListDataByAll();		
					this.request.setAttribute("listloaisanpham", listloaisanpham);
					this.request.setAttribute("listdonvigia", listdonvigia);
					return ActionSupport.SUCCESS;
				}
			}else{
				this.request.setAttribute("errorCode", -1);
			}			
		} catch (Exception ex) {
			this.request.setAttribute("errorCode", -1);
		}
		// return post page
		Paging paging = new Paging();
		ListLoaiSanPham listloaisanpham = LoaiSanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.Display);
		ListDonViGia listdonvigia = DonViGiaService.getListDataByAll();		
		this.request.setAttribute("listloaisanpham", listloaisanpham);
		this.request.setAttribute("listdonvigia", listdonvigia);
		
		this.request.setAttribute("currentdate", SedevUtil.getTime());
		return ActionSupport.ERROR;
	}
	
	public String remove() throws Exception{
		if(this.preExecute() < 0){
			return "denied";
		}
		long id = Long.valueOf(this.request.getParameter("id"));
		if(SanPhamService.removeData(id)){
			this.request.setAttribute("errorCode", 1);			
		}else{
			this.request.setAttribute("errorCode", -1);
		}
		Paging paging = new Paging();
		if(this.request.getParameter("shopping_page_manager") != null){
			paging.setCurrent(Integer.valueOf(this.request.getParameter("shopping_page_manager")));
		}
		paging.setPageSize(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.pagesize","5")));
		paging.setNumDisplay(Integer.valueOf(ConfigUtil.getProperties().getProperty("manager.numdisplay","10")));
		ListSanPham listdata = SanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.All);
		this.request.setAttribute("listsanpham", listdata);
		this.request.setAttribute("paging", paging);
		return ActionSupport.SUCCESS;
	}
	
	public String update() throws Exception{
		if(this.preExecute() < 0){
			return "denied";
		}
		SanPham data = new SanPham();
		try {
			Long id = Long.valueOf(this.request.getParameter("id"));
			if (id != null) {
				data = SanPhamService.getData(id);
				if (data != null && this.initData(data)) {
					if (SanPhamService.updateData(data)) {
						this.request.setAttribute("errorCode", 2);
					} else {
						this.request.setAttribute("errorCode", -1);
					}
					this.request.setAttribute("sanpham", data);
					Paging paging = new Paging();
					ListLoaiSanPham listloaisanpham = LoaiSanPhamService.getListDataByUser(this.ownerId, paging, ModeDisplay.Display);
					ListDonViGia listdonvigia = DonViGiaService.getListDataByAll();		
					this.request.setAttribute("listloaisanpham", listloaisanpham);
					this.request.setAttribute("listdonvigia", listdonvigia);
					return ActionSupport.SUCCESS;
				}
			}
		} catch (Exception ex) {
			return "fail";
		}
		// return fail page
		return "fail";
	}

	private boolean initData(SanPham data){
		data.setUserId(this.ownerId);
		try{
			data.setMaSanPham(this.request.getParameter("productcode"));
		}catch(Exception ex){
			data.setMaSanPham("");
		}
		try{
			data.setTenSanPham(this.request.getParameter("title"));
		}catch(Exception ex){
			data.setTenSanPham("");
		}
		try{
			Long typeId = Long.valueOf(this.request.getParameter("producttype"));
			LoaiSanPham loaisanpham = LoaiSanPhamService.getData(typeId);
			data.setLoaiSanPhamId(typeId);
			data.setNhomSanPhamId(loaisanpham.getNhomSanPhamId());
		}catch(Exception ex){
			data.setLoaiSanPhamId(Long.valueOf(0));
			data.setNhomSanPhamId(Long.valueOf(0));
		}
		try{
			data.setGiaSanPham(Long.valueOf(this.request.getParameter("price")));
		}catch(Exception ex){
			data.setGiaSanPham(Long.valueOf(0));
		}
		try{
			data.setGiaKhuyenMai(Long.valueOf(this.request.getParameter("pricesale")));
		}catch(Exception ex){
			data.setTenSanPham("");
		}
		
		try{
			data.setDonViGia(this.request.getParameter("unitcurrency"));
		}catch(Exception ex){
			data.setDonViGia("VND");
		}
		
		try{
			String vat = this.request.getParameter("productvat");
			if(vat !=null && "on".equals(vat)){
				data.setDaTinhThue(true);
			}else{
				data.setDaTinhThue(false);
			}								
		}catch(Exception ex){
			data.setDaTinhThue(false);
		}
		
		try{
			data.setThuocTinh(this.request.getParameter("attribute"));
		}catch(Exception ex){
			data.setThuocTinh("");
		}
		
		try{
			data.setNoiDung(this.request.getParameter("productcontent"));
		}catch(Exception ex){
			data.setNoiDung("");
		}
		
		try{
			data.setHinhAnh(this.request.getParameter("productimage"));
		}catch(Exception ex){
			data.setHinhAnh("");
		}
		
		try{
			String special = this.request.getParameter("productspecial");
			Integer point = 0;
			if(special!=null && "on".equals(special)){
				point = 1;
			}
			if(point == 0 || data.getVip()== null || (data.getVip()!=null && data.getVip()==0)  ){				
				data.setVip(point);
			}
		}catch(Exception ex){
			data.setVip(0);
		}
		
		try{
			Integer isHienThi = Integer.valueOf(this.request.getParameter("published"));
			if (isHienThi > 0) {
				data.setHienThi(true);
			} else {
				data.setHienThi(false);
			}
		}catch(Exception ex){
			data.setHienThi(false);
		}
		
		try{
			String strdate = this.request.getParameter("date");
			data.setNgayDang(SedevUtil.parseDateToLong(strdate));
		}catch(Exception ex){
			data.setNgayDang(SedevUtil.getTime());
		}
		try{
			Long expire = Long.valueOf(this.request.getParameter("timeexpire"));
			expire = expire * 86400 * 1000;
			if(expire != 0){
				expire = data.getNgayDang() + expire;
			}
			data.setNgayHetHan(expire);
		}catch(Exception ex){
			data.setNgayHetHan(Long.valueOf(0));
		}
		return true;
		
	}
	
}
